<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>电商</title>
</head>
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>public/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>public/css/bootstrap-theme.min.css">
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>public/css/front.css">
<body id="cart_list">
	<div class="container">
		<div class="row" id="header">
			<div class="col-md-3">
				<a href="<?=site_url()?>"><img class="logo" id="logo" src="<?php echo base_url("public/img/logo.jpg")?>" /></a>
			</div>
			<div class="col-md-9">
				<img style="height:50px;" class="pull-right" src="<?=base_url('public/img/shop_step_1.jpg');?>">
			</div>
		</div>
		<br/>
		<form action="<?php echo site_url('Shopping/createorder')?>" method="post">
		<div class="row">
			<div class="col-md-12">
			
				<table class="table table-bordered">
					<tr>
						<th width="80"></th>
						<th>商品</th>
						<th width="150">价格</th>
						<th width="150">商品</th>
						<th width="120">小计</th>
						<th width="80">操作</th>
					</tr>
					<?php 
						$sum=0;
						foreach($carts as $c){
						$product=$this->Products_model->get_one($c->product_id);
						?>
					<tr class="t">
						<td class="text-center">
							<input class="is_select" value="<?=$c->id?>" type="checkbox" name="ids[]">
						</td>
						<input type="hidden" class="product_id" name="product_id" value="<?=$c->product_id?>">
						<td>
							<img style="width:60px;" src="<?=base_url($product->img)?>"><a href="<?=site_url('product/detail/'.$product->id);?>"><?=$product->title?></a>
						</td>
						<td>￥<?=$product->price?></td>
						<td class="cart_num"> 
							<span class="minus">-</span>
							<input name="num" type="text" class="n" value="<?=$c->num?>" />
							<span class="add">+</span>
						</td>
						<td><?= $p=intval($product->price)* intval($c->num)?></td> 
						<?php $sum=$sum+$p;?> 
						<td><a href="<?=site_url('shopping/cartdel/'.$c->id)?>">删除</a></td>
					</tr>
					<?php }?>
					<tr>
						<td class="text-center">
							<div class="checkbox">
								<label>
									<input type="checkbox" name="" id="checkall">全选
								</label>
							</div>
						</td>
						<td colspan="10">
							<a id="delete_all" href="#">删除</a>
							<p class="text-right num">合计:￥<?=$sum?></p>
						</td>
					</tr>
				</table>
				<button id="checkout" class="btn btn-danger pull-right" type="submit">去结算</button>
			</div>
		</div>
	</form>
	</div>
	<script type="text/javascript" src="<?php echo base_url("public/js/jquery.js")?>"></script>
	<script type="text/javascript">
		$(function(){
					 $("#checkall").click(function(){    
		    if(this.checked){    
		        $(".table :checkbox").prop("checked", true);   //全选
		    }else{    
		        $(".table :checkbox").prop("checked", false); //全不选
		    }    
		});
				$('#delete_all').click(function(){
						var ids = [];
						$('.is_select').each(function(i,item){
							if($(item).prop('checked')){
								ids.push($(item).val());
							}
						});
						if(ids.length == 0){
							alert('没有选中任何商品');
						}else{
							$.post("<?=site_url('shopping/cartdels')?>",{
								'ids':ids
							},function(data){
							
								if(data == 'ok'){
									location.reload();
								}
							});
						}
						return false;
					}) 
				$('#checkout').click(function(){
					var data =[];
					$('.is_select').each(function(i,item){
						if($(item).prop('checked')){
							var element={
								'product_id':$(item).parents('.t').find('.product_id').val(),
								'num':$(item).parents('.t').find('.n').val()
							}
							data.push(element);
						}	
					});
					console.log(data);
					$.post('<?=site_url('shopping/preOrder')?>',{
						'data':data
					},function(data){
						if(data=='ok'){
								window.location.href="<?=site_url('shopping/createorder')?>"
						}else{
							alert(data);
						}
					})

						
					return false;
				})
			})
		
	</script>
</body>
</html>	




